<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace HelloAdmin\Controller;

/**
 * Description of MessageController
 *
 * @author gangguo
 */
class MessageController extends CommonController{
    //put your code here

    private $Message;

    public function __construct() {
        parent::__construct();
        $this->Message = new \Common\Model\MessageModel();
    }


    //未读
    public function ready(){



    }

    public function newMessage(){
        $reply_id = I('post.reply_id');
        $re_member_id = I('post.re_member_id');
        if(empty($reply_id)){
            $this->display('Index/newMessage');exit;
        }
        $data = array(
                'message'=>I('post.message'),
                'reply_id'=>$reply_id,
                'member_id'=>1,
                'send_ip'=>get_client_ip(),
                'send_time'=>time(),
                're_member_id'=>$re_member_id,
                'type'=>'mes'
            );

        if($this->Message->add($data)){
            $info = $this->Message->getOneList($reply_id,'member_id');
            $this->Message->updateField('status','3',$reply_id);
            //p($this->Message->getLastSql());exit;
            if($info['member_id'] > 0){
                \Common\Library\COM\MemberDo::addMemberPoint($info['member_id'],5,'提供错误章节信息');
            }
            $this->layerIfreamRe('添加成功');
        }else{
            $this->error('添加失败');
        }

    }


    //已读
    public function already(){


    }
    public function update(){
        if($this->Message->updateField()){
            exit('success');
        }
    }

    public function err(){

        $this->assign('type',I('get.type'));
        $this->display('Index/err');
    }

    public function getErr(){
        $type = I('get.type');
        if(empty($type)){
                         $where = '1=1';
        }else{
            $where = "`type`='{$type}' AND `status`='1'";
        }


        $search = I('post.search');
        $field = I('post.field');
        $d_a = I('post.order');
        if(!empty($search)){
            $where .= " AND `name` like '{$search}%'";
        }

        if(!empty($field) && $d_a){
            $order .= "`{$field}` $d_a";
        }

        $data = $this->Message->getList($where,$order,$this->offset,$this->limit);
        echo (json_encode($data));

    }

    public function mlist(){
        $this->display('Book/already');
    }
}
